//
//  MainViewController.swift
//  SinaMicroBlog
//
//  Created by wxy on 15/9/1.
//  Copyright © 2015年 wxy. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //tabBarButton此时还没有被创建
        addChildViewControllers()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
        //解决按钮不能点击
        super.viewWillAppear(animated)
        //视图资源，只有在需要显示的时候才会被创建，tabBarButton此时已经被创建
        //printLog(tabBar.subviews)
        
        setUpComposedButton()
    }
    
    private func setUpComposedButton() {
        
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        composedButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        //MARK:巧妙创建按钮
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoveryTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    

/// 创建子控制器
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        
        
        vc.title = title
//        vc.navigationItem.title = title
//        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    //MARK: - 懒加载控件
    
    private lazy var composedButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
//        添加到tabBar
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "composedButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    @objc private func composedButtonClicked() {
        
        // 常量有一次设置数值的机会
        let vc: UIViewController
        
        if UserAccountViewModel.sharedUserAccount.userLogin {
            vc = ComposeViewController()
        } else {
            vc = OAuthViewController()
        }
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
}
